﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using ProjectStorageLib.Project;

public partial class CtrlProject : System.Web.UI.UserControl
{
    public Boolean Edit { get; set; }
    public Boolean Editable { get; set; }

    public CtrlProject()
    {
        Edit = false;
    }

    public ProjectManager ProjectManager { get; set; }

    protected void Page_Load(object sender, EventArgs e)
    {
        
    }

    protected void FactorsScale_DataBound(object sender, EventArgs e)
    {
        DropDownList list = (DropDownList)sender;
        System.Web.UI.WebControls.RepeaterItem item =
            (System.Web.UI.WebControls.RepeaterItem)list.Parent;

        var factorScale = ProjectManager.GetScaleValueByFactor(
            (ProjectStorageDB.Factor)item.DataItem);

        if (factorScale != null)
        {
            list.SelectedValue = factorScale.FactorScaleID.ToString();
        }
    }
    protected void FactorsScale_IndexChanged(object sender, EventArgs e)
    {
        DropDownList list = (DropDownList)sender;

        System.Web.UI.WebControls.RepeaterItem item =
            (System.Web.UI.WebControls.RepeaterItem)list.Parent;

        HiddenField factorID = (HiddenField)list.Parent.FindControl("m_hfFactorID");

        if (String.IsNullOrEmpty(list.SelectedValue))
        {
            ProjectManager.DeleteFactor(Decimal.Parse(factorID.Value));
        }
        else
        {
            ProjectManager.SetScaleValueByFactor(
                Decimal.Parse(factorID.Value)
                , Decimal.Parse(list.SelectedValue)
            );
        }
    }

    protected void PracticeRelation_DataBound(object sender, EventArgs e)
    {
        RadioButtonList rbtnList = (RadioButtonList)sender;
        foreach (ListItem rbtn in rbtnList.Items)
        {
            rbtn.Text = "";
        }
        
        System.Web.UI.WebControls.RepeaterItem item =
            (System.Web.UI.WebControls.RepeaterItem)rbtnList.Parent;

        var agilePractice = ProjectManager.GetPracticeValueByPractice(
            (ProjectStorageDB.AgilePractice)item.DataItem);

        if (agilePractice != null)
        {
            rbtnList.SelectedValue = agilePractice.WrappedValue.ToString();
        }
    }
    protected void PracticeRelation_IndexChanged(object sender, EventArgs e)
    {
        RadioButtonList list = (RadioButtonList)sender;

        System.Web.UI.WebControls.RepeaterItem item =
            (System.Web.UI.WebControls.RepeaterItem)list.Parent;

        HiddenField practiceID = (HiddenField)list.Parent.FindControl("m_hfPracticeID");

        if (String.IsNullOrEmpty(list.SelectedValue))
        {
            ProjectManager.DeletePractice(Decimal.Parse(practiceID.Value));
        }
        else
        {
            ProjectManager.SetPracticeValueByFactor(
                Decimal.Parse(practiceID.Value)
                , (ProjectStorageDB.Objects.EnumPracticeUsingValue)
                    Enum.Parse(typeof(ProjectStorageDB.Objects.EnumPracticeUsingValue), list.SelectedValue)
            );
        }
    }

    public void FillFromRequest()
    {
        ProjectManager.Project.Name = m_ctrlName.Text;
        ProjectManager.Project.Description = m_ctrlDescription.Text;
    }

    protected void Save_Click(object sender, EventArgs e)
    {
        if (this.Page.IsValid)
        {
            FillFromRequest();
            ProjectManager.Save();

            if (Saved != null)
            {
                Saved.Invoke(this, e);
            }
        }
    }
    protected void Cancel_Click(object sender, EventArgs e)
    {
        if (Cancel != null)
        {
            Cancel.Invoke(this, e);
        }
    }

    protected void m_ctrlName_Validate(object sender, ServerValidateEventArgs e)
    {
        //if (!Edit)
            e.IsValid = !ProjectManager.ExistsProject(e.Value);  
    }

    public event EventHandler Saved;
    public event EventHandler Cancel;

}